package com.wfg.internet;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.AsyncHandlerInterceptor;



/**
 * @author: 王富贵
 * @description: 日志拦截器，把唯一id号放入ThreadLocal
 * @createTime: 2022年12月05日 15:41:52
 */
@Component
public class LogInterceptor implements AsyncHandlerInterceptor {
    /**
     * 执行处理程序之前，注入唯一ID
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 注入一个Entry，value就是请求流水号
        MDC.put("trance", request.getRequestURI());
        return true;
    }

    /**
     * 处理了之后
     */
    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) {
        MDC.clear();
    }
}
